﻿<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<c:set var="path" value="${pageContext.request.contextPath }" />
<!DOCTYPE HTML>
<html>
<head>  
<meta charset="utf-8">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<meta name="renderer" content="webkit|ie-comp|ie-stand">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport"
	content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<LINK rel="Bookmark" href="../images/favicon.ico">
<!-- 本框架基本脚本和样式 -->
<script type="text/javascript"
	src="${path }/resource/js/jquery-2.1.4.min.js"></script>
<script type="text/javascript"
	src="${path }/resource/js/systools/MBase.js"></script>

<!-- 界面单独引入的其他样式和脚本 -->
<link rel="stylesheet"
	href="${path }/resource/plugin/zTree/zTreeStyle/zTreeStyle.css"
	type="text/css">
<script type="text/javascript"
	src="${path }/resource/plugin/zTree/jquery.ztree.core-3.5.min.js"></script>
<script type="text/javascript"
	src="${path }/resource/plugin/zTree/jquery.ztree.excheck.min.js"></script>


</head>
<style>
.permission-list>dd>dl>dt {width：140px；
	
}
</style>
<body>
	<div class="ibox-content">
		<form class="form-horizontal" id="dataform"
			onsubmit="javascripr:return false;">
		<input type="hidden" name="tokenUrl" value="${tokenUrl}">	 
		<input type="hidden" name="token" value="${token}">
			<input type="hidden" name="companyId" value=${user.companyId} >
			<c:if test="${obj ne null }">
				<input type="hidden" name="id" value="${obj.id }">
			</c:if>
			<div class="form-group">
				<label class="col-sm-2 control-label">角色名称：</label>
				<div class="col-sm-8">
					<input type="form-control" dataType="*1-50" class="form-control"
						value="${obj.name }" name="name" datatype="*4-16"
						nullmsg="角色名称不能为空">
					<div class="Validform_checktip"></div>
				</div>
			</div>
			<div class="form-group ">
				<label class="col-sm-2 control-label">备注：</label>
				<div class="col-sm-8">
					<input type="text" class="form-control" id="rmark" name="remark"
						value="${obj.remark }">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-2 control-label">功能权限</label>
				<div class="  col-sm-8" id="powerBox"></div>
			</div>

			<div class="form-group ">
				<div class="col-sm-12 text-center">
					<a href="javascript:;" onclick="myForm.submit()"
						class="btn btn-success radius">保存</a>&nbsp;&nbsp;&nbsp;&nbsp; <button onclick="MTools.closeForm()" class="btn btn-danger radius"
					type="button">取消</button>
				</div>
			</div>
		</form>
	</div>
	<script type="text/javascript">
		<c:if test="${obj ne null }">
		var targetId = "${obj.id}";
		</c:if>

		$(function() {
			myForm = MForm.initForm({
				invokeUrl : "${path}/admin/role/addOrModify",
				afterSubmit : function() {
					parent.myGrid.serchData();
				},
			});
			initFunctionList();
			<c:if test="${obj ne null }">
			initFormData();
			</c:if>
		});

		// 初始话表单内部数据
		function initFormData() {

			var functions = [];
			var functionsBtn = {};

			$
					.AjaxProxy({
						p : {
							roleId : targetId
						},
						a : false,
						c : true
					})
					.invoke(
							"${path}/admin/role/findRolePwoer",
							function(loj) {
								for (var i = 0; i < loj.getRowCount(); i++) {
									var id = loj.getString(i, "functionId");
									var btns = loj.getString(i, "btns");
									functions[i] = id + "";
									functionsBtn[id] = btns;
								}
								// 添加一级功能权限
								$("body").find(".firstFunction").each(
										function() {
											var functionId = $(this);
											if (functions.indexOf(functionId
													.val()) != -1) {
												functionId
														.prop("checked", true);
											}
										});
								// 添加二级功能权限
								$("body")
										.find(".secondFunction")
										.each(
												function() {

													var functionId = $(this);
													if (functions
															.indexOf(functionId
																	.val()) != -1) {
														functionId
																.prop(
																		"checked",
																		true);
													}

													$(this)
															.closest("dl")
															.find(
																	"dd input:checkbox")
															.each(
																	function() {
																		var btnId = $(this);
																		if (functionsBtn[functionId
																				.val()] != null
																				&& (functionsBtn[functionId
																						.val()] + ",")
																						.indexOf(btnId
																								.val()
																								+ ",") != -1) {
																			btnId
																					.prop(
																							"checked",
																							true);
																		}
																	});
												});
							});
		}
		var index = 0;
		// 初始化功能节点
		function initFunctionList() {

			// 查询一级功能
			$
					.AjaxProxy({
						a : false,
						c : false
					})
					.invoke(
							'${path}/admin/function/topFunction',
							function(loj1) {
								var html = '';
								for (var i = 0; i < loj1.getRowCount(); i++) {

									html += '<dl class="permission-list"><dt><label  >'
											+ '	<input class="firstFunction" type="checkbox" value="'
											+ loj1.getString(i, "id")
											+ '" name="pwoers['
											+ index
											+ '].functionId" id="user-Character-0">'
											+ loj1.getString(i, "name")
											+ '</label></dt><dd>';
									index++;
									// 查询二级功能---------------------------------------------------------------------
									$
											.AjaxProxy(
													{
														p : {
															parentId : loj1
																	.getString(
																			i,
																			"id")
														},
														a : false,
														c : false
													})
											.invoke(
													'${path}/admin/function/findByModel',
													function(loj2) {
														for (var j = 0; j < loj2
																.getRowCount(); j++) {

															html += '<dl class="cl permission-list2"><dt><label class=""> '
																	+ '<input class="secondFunction" type="checkbox" value="'
																	+ loj2
																			.getString(
																					j,
																					"id")
																	+ '" name="pwoers['
																	+ index
																	+ '].functionId" id="user-Character-0-0">'
																	+ '	'
																	+ loj2
																			.getString(
																					j,
																					"name")
																	+ '</label></dt><dd>';

															// 查询功能按钮---------------------------------------------------------------------
															$
																	.AjaxProxy(
																			{
																				p : {
																					functionId : loj2
																							.getString(
																									j,
																									"id")
																				},
																				a : false,
																				c : false
																			})
																	.invoke(
																			'${path}/admin/btn/functionBts',
																			function(
																					loj3) {
																				for (var k = 0; k < loj3
																						.getRowCount(); k++) {
																					html += '<label class="">'
																							+ '<input type="checkbox" value="'
																							+ loj3
																									.getString(
																											k,
																											"btnValue")
																							+ '" name="pwoers['
																							+ index
																							+ '].btns" id="user-Character-0-0-0">'
																							+ loj3
																									.getString(
																											k,
																											"btnKey")
																							+ '</label>';
																				}

																			});
															html += '</dd></dl>';
															index++;
														}
													});
									html += '</dd></dl>';
								}

								$("#powerBox").html(html);
								initEvent();
							});

		}

		// 初始化点击事件
		function initEvent() {
			//点击一级目录
			$(".permission-list>dt input:checkbox").click(
					function() {
						$(this).closest("dl").find("dd input:checkbox").prop(
								"checked", $(this).prop("checked"));
					});
			//点击二级目录
			$(".permission-list2>dt input:checkbox")
					.click(
							function() {
								$(this).closest("dl").find("dd input:checkbox")
										.prop("checked",
												$(this).prop("checked"));
								var l2 = $(this).parents(".permission-list")
										.find(".permission-list2 dd").find(
												"input:checked").length;
								if (l2 == 0) {
									$(this).closest("dd").prev("dt").find(
											"input:checkbox").prop("checked",
											false);
								} else {
									$(this).closest("dd").prev("dt").find(
											"input:checkbox").prop("checked",
											true);
								}

							});

			//点击按钮 判断了按钮为0的情况如果按钮为0则取消当前二级目录选择，同时还判断是否取消一级目录
			$(".permission-list2 dd input:checkbox")
					.click(
							function() {
								var l = $(this).parent().parent().find(
										"input:checked").length;
								var l2 = $(this).parents(".permission-list")
										.find(".permission-list2 dd").find(
												"input:checked").length;
								if ($(this).prop("checked")) {
									$(this).closest("dl").find(
											"dt input:checkbox").prop(
											"checked", true);
									$(this).parents(".permission-list").find(
											"dt").first()
											.find("input:checkbox").prop(
													"checked", true);
								} else {
									if (l == 0) {
										$(this).closest("dl").find(
												"dt input:checkbox").prop(
												"checked", false);
									}
									if (l2 == 0) {
										$(this).parents(".permission-list")
												.find("dt").first().find(
														"input:checkbox").prop(
														"checked", false);
									}
								}

							});
		}
	</script>
</body>
</html>